<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../include/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>角色关联权限页面</title>
<%@ include file="../include/easyui.jsp"%>
</head>
<body>
<span id="permissionTree"></span>
<script type="text/javascript">
//加载整个权限树
var turnOn;//开关，turnOn为false时不执行onCheck事件，
var permissionTree = $('#permissionTree').tree({
    url: '${ctx}/system/permission/tree/${roleId}',
    method: 'get',
    parentField: 'parentId',
    animate: true,//显示动画效果
    lines: true,
    checkbox: true,
    cascadeCheck: false,
    onlyLeafCheck: false, //只有子节点有复选框
    onClick: function(node) {
        //点击后触发
    },
    onCheck: function (node, checked) {
        if (checked) {
            var parentNode = permissionTree.tree('getParent', node.target);
            if (parentNode != null) {
                permissionTree.tree('check', parentNode.target);
            }
        } else {
            var childNode = permissionTree.tree('getChildren', node.target);
            if (childNode.length > 0) {
                for (var i = 0; i < childNode.length; i++) {
                    permissionTree.tree('uncheck', childNode[i].target);
                }
            }
        }
    },
    onLoadSuccess: function(node, data) {
        /**
        //选中已经有的权限 - 已移到后台实现
        $.post('${ctx}/system/role/${roleId}/permission', function(data) {
            if(data){
                for(var i = 0; i < data.length; i++){
                    var node = permissionTree.tree('find', data[i].permissionId);
                    if (node) {
                        permissionTree.tree('check', node.target);
                    }
                }
            }
        }, 'json');*/
    }
});

//保存授权信息
function saveRolePermission(){
    //state值有三个：'checked','unchecked','indeterminate'，如果state值未指定，则默认为checked
    var checkNodes = permissionTree.tree('getChecked',['checked','indeterminate']);
    var newPermList = [];
    if (checkNodes && checkNodes.length > 0) {
        for (var i = 0; i < checkNodes.length; i++) {
            newPermList.push(checkNodes[i].id);
        }
    }

	$.ajax({
		async:false,
		type:'POST',
		data:JSON.stringify(newPermList),
		contentType:'application/json;charset=utf-8',	//必须
		url:"${ctx}/system/role/${roleId}/assignPermission",
		success: function(data){
			if(data.message=='success'){
				parent.$.messager.show({ title : "提示",msg: "操作成功！", position: "bottomRight" });
			}else{
				$.easyui.messager.alert(data.message);
			}
		}
	});
}
</script>
</body>
</html>